﻿using System;
using System.Collections.Generic;
#if DEBUG
using System.Diagnostics;
#endif // #if DEBUG
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using FRL.DebtChased.Repository;
using FRL.DebtChased.Website;

namespace WebApplication.Finance
{

	/// <summary>
	/// Declaration of CommTemplateEmailEditor
	/// </summary>
	public partial class CommTemplateEmailEditor : System.Web.UI.Page
	{

		/////////////////////////////////////////////////////////////
		// Nested classes

		[Serializable]
		internal class Model
		{

			/////////////////////////////////////////////////////////
			// Attributes

			public int CommTemplateId;

			public Definitions.eCommTemplateType TemplateType = Definitions.eCommTemplateType.NotSet;

		}

		/////////////////////////////////////////////////////////////
		// Constants

		private const string ModelTag = "ContextModel";

		/////////////////////////////////////////////////////////////
		// Attributes

		private Model ContextModel
		{
			get { return ViewState[ModelTag] as Model; }
			set { ViewState[ModelTag] = value; }
		}

		/////////////////////////////////////////////////////////////
		// Event handlers

		protected override void OnPreRender(EventArgs e)
		{

			// Call base-class function
			base.OnPreRender(e);

			// Get the model
			Model model = ContextModel;
#if DEBUG
			Debug.Assert(model != null);
#endif // #if DEBUG
			if (model.TemplateType == Definitions.eCommTemplateType.NotSet)
			{

				// If there is a template id...
				if (model.CommTemplateId != 0)
				{

					// Load the template details
					string strName;
					string strBody;
					using (DebtChasedEntities ent = new DebtChasedEntities())
					{
						CommTemplate.Get(
							ent,
							model.CommTemplateId,
							out strName,
							out model.TemplateType,
							out strBody
						);
					}

					// Set controls
					txtName.Text = strName;
					richEditor.Content = strBody;

				}
				else
				{

					// A new template
					model.TemplateType = Definitions.eCommTemplateType.Email;

				}

			}

			// Enable/disable buttons
			butRevert.Enabled = (model.CommTemplateId != 0);

		}

		protected void Page_Load(object sender, EventArgs e)
		{

			// If this is a postback...
			if (this.IsPostBack)
			{

			}
			else
			{

				// Recover the template id
				MySession mySession = MySession.BindMySession();
				int? iCommTemplateId = mySession.PullHandover(this);
				if (iCommTemplateId == null)
				{
					Response.Redirect("~/Finance/CommTemplates.aspx");
					return;
				}
				if (iCommTemplateId.Value == -1)
					iCommTemplateId = 0;

				// Create the model
				Model model = new Model();
				ContextModel = model;
				model.CommTemplateId = iCommTemplateId.Value;

				// Colour the panels
				MyApplication myApp = MyApplication.BindMyApplication();
				pnlControls.BackColor = myApp.PrimaryWebColor;

			}

		}

		protected void butTest_Click(object sender, EventArgs e)
		{

			// Development message
			(this.Master as FinanceMaster).ErrorMessage = Definitions.DevelopmentMessage;

		}

		protected void butSave_Click(object sender, EventArgs e)
		{

			// Validate content
			this.Validate();
			if (!this.IsValid)
			{
				(this.Master as FinanceMaster).ErrorMessage = this.Validators.ToErrorMessage();
				return;
			}

			// Get user id
			MySession mySession = MySession.BindMySession();
			int iUserId = mySession.UserID;
#if DEBUG
			if (iUserId == 0)
				iUserId = 1;
#endif // #if DEBUG

			// Get the model
			Model model = ContextModel;
#if DEBUG
			Debug.Assert(model != null);
			Debug.Assert(model.TemplateType == Definitions.eCommTemplateType.Email);
#endif // #if DEBUG

			// Get control values
			string strName = txtName.Text;
			string strBody = richEditor.Content;

			// Update the repository
			string strError;
			using (DebtChasedEntities ent = new DebtChasedEntities())
			{
				strError = CommTemplate.Set(ent, model.CommTemplateId, model.TemplateType, strName, strBody, iUserId);
			}
			if (strError != null)
				(this.Master as FinanceMaster).ErrorMessage = strError;
			else
				Response.Redirect("~/Finance/CommTemplates.aspx");

		}

		protected void butRevert_Click(object sender, EventArgs e)
		{

			// Reload the model
			ContextModel.TemplateType = Definitions.eCommTemplateType.NotSet;

		}

	}

}